#include "Figur.h"

Figur::Figur()
{
}

Figur::Figur(Graphics ** gr,env_element ** cur,  int * scroll1, int * scroll2)
{
	scrollx = scroll1;
	scrolly = scroll2;
	cur_env = cur;
	GR = gr;
	
	this->change_Animation(WALK);

	touchdown = true;
	moving = 0;
	anim_count = 0;
	direct = 0;
	x = 200;
	y = 100;
	al_draw_bitmap_region(cur_Animation,0,0,framex,framey,0,0,0);
	Hitbox.x1 = -30;
	Hitbox.y1 = -180;
	Hitbox.x2 = 30;
	Hitbox.y2 = 0;
	yspeed = 0.0;
	xspeed = 0;
}

void Figur::draw(ALLEGRO_DISPLAY ** display)
{
	al_draw_tinted_bitmap_region(cur_Animation,al_map_rgba(255,255,255,255),framex * (anim_count /ANIMDELAY),0,framex,framey,x-scrollx[0] -(framex/2),y-scrolly[0]-framey,direct);
	int tmpx = 0;
	int tmpy = 0;
	double xapp = 0.0;
	double yapp = 0.0;
	if(xspeed > 0)
	{
		tmpx = ceil(xspeed);
		xapp = xspeed - tmpx*1.0;
		if(xapp < 0.3)
		{
			xspeed = tmpx * 1.0;
		}
	}
	else
	{
		tmpx = floor(xspeed);
		xapp = xspeed - tmpx*1.0;
		if(xapp < -0.3)
		{
			xspeed = tmpx * 1.0;
		}
	}
	if(yspeed > 0)
	{
		tmpy = ceil(yspeed);
		yapp = yspeed - tmpy*1.0;
		if(yapp < 0.3)
		{
			yspeed = tmpy * 1.0;
		}
	}
	else
	{
		tmpy = floor(yspeed);
		yapp = yspeed - tmpy*1.0;
		if(yapp < -0.3)
		{
			yspeed = tmpy * 1.0;
		}
	}

	int * buffer = cur_env[0]->get_collision(Hitbox,x,y,tmpx,tmpy,direct);
	if(buffer[0] == 1)
	{
		if(buffer[2] == 0)
		{
			xspeed = 0.0;
		}
		else
		{
			xspeed = buffer[1]*1.0;
			if(xspeed < 0)
			{
				xspeed += 1.0;
			}
			else if(xspeed > 0)
			{
				xspeed -= 1.0;
			}
		}
	}
	if(buffer[2] == 1)
	{
		yspeed = buffer[3]*1.0;
		if(buffer[3] >= 0)
		{
			touchdown = true;
			moving = 0;
		}
	}
	else
	{
		touchdown = false;
		moving = 3;
	}

	if(!touchdown)
	{
		yspeed += ACCELLERATE;
		if(moving == 3)
		{
			if(anim_count < 8*ANIMDELAY)
			{
				anim_count ++;
			}
		}
	}

	x += xspeed;
	y += yspeed;
	
}

void Figur::move_r()
{
	this->change_Animation(WALK);
	if(touchdown)
	{
		if(xspeed < 0)
		{
			xspeed = 0;
		}
		direct = 0;
		
		if(xspeed <= XMAX)
		{
			xspeed += XACCEL;
		}
		anim_count ++;
		if(anim_count > anim_max *ANIMDELAY -1)
		{
			anim_count = 0;
		}
	}
}
void Figur::move_l()
{
	this->change_Animation(WALK);
	if(touchdown == true)
	{
		if(xspeed > 0)
		{
			xspeed = 0;
		}
		direct = 1;
		if(xspeed >= -XMAX)
		{
			xspeed -= XACCEL;
		}
		anim_count ++;
		if(anim_count > anim_max*ANIMDELAY -1)
		{
			anim_count = 0;
		}
	}
}

void Figur::jump()
{
	if(touchdown)
	{
		touchdown = false;
		moving = 3;
		yspeed = -20.0;
		this->change_Animation(JUMP);
		anim_count = 0;
	}
}

void Figur::set_touchdown(bool val)
{
	touchdown = val;
}

void Figur::stop()
{
	xspeed = 0;
	moving = 0;
	anim_count = 0;
}

int Figur::get_moving()
{
	return moving;
}

bool Figur::get_touchdown()
{
	return touchdown;
}

void Figur::set_env(env_element ** derp)
{
	cur_env = derp;
}

void Figur::change_Animation(int anim)
{
	cur_Animation = GR[0]->get_bmp(0,anim);
	anim_max = al_get_bitmap_width(cur_Animation) / 120;
	framey = al_get_bitmap_height(cur_Animation);
	framex = 120;
}

int Figur::get_x()
{
	return x;
}

int Figur::get_y()
{
	return y;
}

int Figur::get_direction()
{
	return direct;
}


Figur::~Figur()
{

}